package com.ruoyi.web.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.exception.TadpoleException;
import com.ruoyi.web.domain.SmsSendRecord;
import com.ruoyi.web.excel.SmsSendRecordExcel;
import com.ruoyi.web.service.ISmsSendRecordService;
import com.ruoyi.web.utils.CommonUtil;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 短信发送记录Controller
 *
 * @date 2023-03-28
 */
@RestController
@RequestMapping("/smsSendRecord")
@AllArgsConstructor
public class SmsSendRecordController extends BaseController {
    private final ISmsSendRecordService smsSendRecordService;

    /**
     * 查询短信发送记录列表
     */
    @PreAuthorize("@ss.hasPermi('smsSendRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmsSendRecord smsSendRecord) {
        startPage();
        List<SmsSendRecord> list = smsSendRecordService.selectSmsSendRecordList(smsSendRecord);
        return getDataTable(list);
    }

    /**
     * 导出短信发送记录列表
     */
    @PreAuthorize("@ss.hasPermi('smsSendRecord:export')")
    @Log(title = "短信发送记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmsSendRecord smsSendRecord) {
        List<SmsSendRecord> list = smsSendRecordService.selectSmsSendRecordList(smsSendRecord);
        ExcelUtil<SmsSendRecord> util = new ExcelUtil<SmsSendRecord>(SmsSendRecord.class);
        util.exportExcel(response, list, "短信发送记录数据");
    }

    /**
     * 获取短信发送记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('smsSendRecord:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(smsSendRecordService.selectSmsSendRecordById(id));
    }

    /**
     * 新增短信发送记录
     */
    @PreAuthorize("@ss.hasPermi('smsSendRecord:add')")
    @Log(title = "短信发送记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(MultipartFile file) {
        SysUser user = CommonUtil.getLoginUserNotNull();

        try {
            List<SmsSendRecordExcel> list = new ExcelUtil<>(SmsSendRecordExcel.class).importExcel(file.getInputStream());
            if (CollectionUtils.isEmpty(list)) {
                throw new TadpoleException("没有检测到任何数据");
            }

            SmsSendRecordExcel first = list.get(0);
            if (Strings.isBlank(first.getContent())) {
                throw new TadpoleException("消息内容不能为空");
            } else if (first.getContent().length() > 1600) {
                throw new TadpoleException("消息内容长度最大为1600");
            }

            smsSendRecordService.add(list, user);
        } catch (Throwable e) {
            throw new TadpoleException("解析Excel数据失败，原因：" + e.getMessage());
        }

        return success();
    }

    /**
     * 修改短信发送记录
     */
    @PreAuthorize("@ss.hasPermi('smsSendRecord:edit')")
    @Log(title = "短信发送记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmsSendRecord smsSendRecord) {
        return toAjax(smsSendRecordService.updateSmsSendRecord(smsSendRecord));
    }

    /**
     * 删除短信发送记录
     */
    @PreAuthorize("@ss.hasPermi('smsSendRecord:remove')")
    @Log(title = "短信发送记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(smsSendRecordService.deleteSmsSendRecordByIds(ids));
    }
}
